package com.ylxl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ylxl.common.annotation.Log;
import com.ylxl.common.enums.BusinessType;
import com.ylxl.common.result.AjaxResult;
import com.ylxl.common.result.BaseController;
import com.ylxl.common.utils.StringUtils;
import com.ylxl.entity.Label;
import com.ylxl.entity.User;
import com.ylxl.entity.request.*;
import com.ylxl.service.ILabelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标签管理
 * @Auther Gilbert
 * @Date 2023/6/25 18:53
 */
@Slf4j
@RestController
@RequestMapping("/label")
@Api(tags = "标签管理", hidden = true)
public class LabelController extends BaseController {

    @Resource
    private ILabelService labelService;

    /**
     * 分页查询标签列表信息
     * @param queryLabel
     * @return
     */
    @Log(title = "分页查询标签列表信息", businessType = BusinessType.SELECT)
    @PostMapping("/queryLabelList")
    @ApiOperation(value = "分页查询标签列表信息", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Label.class),})
    public AjaxResult queryLabelList(@RequestBody QueryLabel queryLabel) {
        AjaxResult ajax = AjaxResult.success();
        try {
            //进行分页操作
            PageInfo<Label> pageInfo = new PageInfo<>();
            if (StringUtils.isNotEmpty(queryLabel.getPageNum()) && StringUtils.isNotEmpty(queryLabel.getPageSize())) {
                PageHelper.startPage(queryLabel.getPageNum(), queryLabel.getPageSize());
            }
            List<Label> labels = labelService.selectLabelList(queryLabel);
            pageInfo = new PageInfo<>(labels);
            ajax.put(AjaxResult.DATA_TAG, labels);
            ajax.put(AjaxResult.TOTAL, pageInfo.getTotal());
        } catch (Exception e) {
            log.error("查询标签列表日志信息，异常信息：{}", e);
            return toAjax(false);
        }
        return ajax;
    }

    /**
     * 标签管理保存
     * @param labelCmdOrMdy
     * @return
     */
    @Log(title = "标签管理保存", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    @ApiOperation(value = "标签管理保存", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult add(@RequestBody LabelCmdOrMdy labelCmdOrMdy){
        if (labelService.checkNameUnique(labelCmdOrMdy)){
            return error("新增标签" + labelCmdOrMdy.getLabelName() + "'失败，标签已存在");
        }
        return toAjax(labelService.insertLabel(labelCmdOrMdy));
    }

    /**
     * 标签管理编辑
     * @param labelCmdOrMdy
     * @return
     */
    @Log(title = "标签管理编辑", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation(value = "标签管理编辑", response = AjaxResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult update(@RequestBody LabelCmdOrMdy labelCmdOrMdy){
        if (labelService.checkNameUnique(labelCmdOrMdy)){
            return error("新增标签" + labelCmdOrMdy.getLabelName() + "'失败，标签已存在");
        }
        return toAjax(labelService.updateLabel(labelCmdOrMdy));
    }
    /**
     * 标签根据id查询明细
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "标签根据id查询明细", response = AjaxResult.class)
    @Log(title = "标签根据id查询明细", businessType = BusinessType.SELECT)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Label.class),})
    public AjaxResult getInfo(@PathVariable(value = "id", required = true) Integer id) {
        return AjaxResult.success(labelService.selctById(id));
    }
    /**
     * 标签根据id删除
     */
    @PostMapping("/remove/{id}")
    @ApiOperation(value = "标签根据id删除", response = AjaxResult.class)
    @Log(title = "标签根据id删除", businessType = BusinessType.DELETE)
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = Integer.class),})
    public AjaxResult remove(@PathVariable(value = "id", required = true) Integer id) {
        return AjaxResult.success(labelService.deleteById(id));
    }
}
